﻿
Partial Public Class mainForm
#Region "声明"
    Private myGraphic, selectarea As Graphics '图形，不然怎么画？
    Private pens As Pen
    Private currentfilename As String = ""  '文件名
    Private fileNameExt As String
    Private history As Integer '历史纪录
    Private BooleanmouseDown As Boolean = False '是否画
    Public Selectedpath As New GraphicsPath '选区
    Private brushimage As Image = Nothing  '笔刷图案
    Private brushsize As Single = 1 '笔刷大小
    Private tooltype As New tool.ToolType   '选择工具
    Private EllipseSelect As tool.RectangleSelectTool 'EllipseSelect
    Private RectangleSelect As tool.RectangleSelectTool
    'RectangleSelectTool
    Private LassoSelectTool As tool.LassoSelectTool
    'LassoSelectTool
    Private lastpoint As New PointF '记录鼠标坐标
    Private nowpoint As New PointF '记录鼠标坐标
    Private PaintBucket As tool.PaintBucketTool
    Private pendraw As pendraw.pendraw
    Private pendrawImage As pendraw.Imagebrush
    Private tmpFolder As String
    '笔刷画
#End Region
#Region "通用属性"
    '选区考虑是否为空
    Public ReadOnly Property SelectedRegion() As Region
        Get
            If (Integer.Equals(Selectedpath.PointCount, 0)) Then
                Return New Region(New Rectangle(0, 0, PictureBoxCanvas.Size.Width, PictureBoxCanvas.Size.Height))
            Else
                Return New Region(Selectedpath)
            End If
        End Get
    End Property
    Private ReadOnly Property bitmapready() As Boolean '确定是否 加载了图像
        Get
            If Image.Equals(Me.PictureBoxCanvas.Image, Nothing) OrElse Not Me.PictureBoxCanvas.Visible Then
                Return False
            Else
                Return True
            End If
        End Get
    End Property
    '获得所选图像
    Public ReadOnly Property selectimage() As Image
        Get
            Using srcImage As Bitmap = New Bitmap(Me.PictureBoxCanvas.Image)
                Dim it As ImageTransform.ImageTransform = New ImageTransform.ImageTransform()
                Return (it.Crop(srcImage, Me.SelectedRegion))
            End Using

        End Get
    End Property
#End Region
#Region "通用函数"
    'dispose the Graphics
    Private Sub killGraphics(ByRef objects As Graphics)
        If Not Object.Equals(objects, Nothing) Then
            objects.Dispose()
        End If
    End Sub
    Private Sub killpen(ByRef objects As Pen)
        If Not Object.Equals(objects, Nothing) Then
            objects.Dispose()
        End If
    End Sub
    'newselectpath新选区
    Private Sub newselectpath()
        clearselectToolStripMenuItem_Click(Nothing, Nothing)
        selectarea = Me.PictureBoxCanvas.CreateGraphics()
    End Sub
    '启动
    Private Sub showworkspace()
        ColorToolStripMenuItem.Visible = True
        DrawToolStripMenuItem.Visible = True
        editToolStripMenuItem.Visible = True
        imageToolStripMenuItem.Visible = True
        SelectToolStripMenuItem.Visible = True
        PictureBoxCanvas.Visible = True
        printPreviewToolStripMenuItem.Visible = True
        printToolStripMenuItem.Visible = True
        segmentationToolStripMenuItem.Visible = True
        effectToolStripMenuItem.Visible = True
        edgeDetectToolStripMenuItem.Visible = True
        Me.Text = "画画"
    End Sub
    Private Sub hideworkspace()
        ColorToolStripMenuItem.Visible = False
        DrawToolStripMenuItem.Visible = False
        editToolStripMenuItem.Visible = False
        imageToolStripMenuItem.Visible = False
        SelectToolStripMenuItem.Visible = False
        PictureBoxCanvas.Visible = False
        printPreviewToolStripMenuItem.Visible = False
        printToolStripMenuItem.Visible = False
        segmentationToolStripMenuItem.Visible = False
        effectToolStripMenuItem.Visible = False
        edgeDetectToolStripMenuItem.Visible = False
        Me.PictureBoxCanvas.Visible = False
        currentfilename = String.Empty
        Me.Text = "画画"
        newfile()
    End Sub
    '新文件
    Private Sub newfile(ByRef bitmapready As Boolean)
        If Not bitmapready Then
            showworkspace()
            bitmapready = True
            Return
        End If
        newfile()
        Form1_Load(Nothing, Nothing)
        holdToolStripMenuItem_Click(Nothing, Nothing)
    End Sub
    Private Sub newfile()
        killGraphics(Me.myGraphic)
        killGraphics(selectarea)
        tooltype = Nothing
        clearselectToolStripMenuItem_Click(Nothing, Nothing)
    End Sub
    '画选区
    Private Sub DrawSelectedpath()
        If Object.Equals(Me.Selectedpath, Nothing) Then
            Return
        End If
        selectarea.DrawPath(Drawing.Pens.White, Me.Selectedpath)
        redraw()
    End Sub
    Private Sub DrawSelectedpath(ByVal notredraw As Boolean)
        If Object.Equals(Me.Selectedpath, Nothing) Then
            Return
        End If
        selectarea.DrawPath(Drawing.Pens.White, Me.Selectedpath)
    End Sub
    Private Sub drawRegion()
        selectarea.FillRegion(New SolidBrush(Color.FromArgb(60, Color.Cyan)), SelectedRegion)
    End Sub

    Private Sub redraw()
        If Image.Equals(Me.PictureBoxCanvas.Image, Nothing) Then
            Return
        Else
            Me.PictureBoxCanvas.Refresh()
        End If
    End Sub

    Private Sub loadhistory(ByVal holdfile As String)
        If System.IO.File.Exists(holdfile) Then
            Me.PictureBoxCanvas.ImageLocation = holdfile
            Me.PictureBoxCanvas.Load()
            newfile()
        End If
    End Sub
    Private Sub saveimage(ByVal FileName As String, ByVal fileFormat As String)

        Try
            Using a As Bitmap = New Bitmap(Me.PictureBoxCanvas.Image)

                Select Case fileFormat
                    Case ".jpg"
                        a.Save(FileName, Imaging.ImageFormat.Jpeg)
                    Case ".gif"
                        a.Save(FileName, Imaging.ImageFormat.Gif)
                    Case ".bmp"
                        a.Save(FileName, Imaging.ImageFormat.Bmp)
                    Case ".png"
                        a.Save(FileName, Imaging.ImageFormat.Png)
                    Case ".tif"
                        a.Save(FileName, Imaging.ImageFormat.Tiff)
                    Case Else
                        Return
                End Select
            End Using
        Catch ex As Runtime.InteropServices.ExternalException
            MessageBox.Show("保存失败！该图像以错误的图像格式保存" & ex.Message, "保存失败。", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "保存失败！", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
    Private Sub saverawimage(ByVal FileName As String)

        Try
            Using a As Bitmap = New Bitmap(Me.PictureBoxCanvas.Image)
                a.Save(FileName, a.RawFormat)
            End Using
        Catch ex As Runtime.InteropServices.ExternalException
            MessageBox.Show("保存失败！该图像以错误的图像格式保存" & ex.Message, "保存失败。", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "保存失败！", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub overwritebitmap(ByRef dust As PictureBox, ByRef image As Bitmap)
        If Not Object.Equals(dust.Image, Nothing) Then
            If Object.Equals(dust, Nothing) OrElse Object.Equals(image, Nothing) OrElse Not MessageBox.Show("要覆盖这个图像？", "要覆盖这个图像？", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes OrElse Object.Equals(dust, Nothing) OrElse Object.Equals(image, Nothing) Then
                Return
            End If
        End If
        dust.Image = image
    End Sub
#End Region
End Class